home *** CD-ROM | disk | FTP | other *** search
- #!/usr/local/bin/perl5
- #
- # count_pf.perl5 -- run lsof in repeat mode and count processes and
- # files
-
- sub interrupt { print "\n"; exit 0; }
-
- $LSOF = "../lsof"; # path to lsof
- $RPT = 15; # lsof repeat time
-
- if ( ! -x $LSOF) { print "can't execute $LSOF\n"; exit 1 }
-
- # Read lsof -HPF0 output repeatedly from a pipe.
-
- $| = 1; # unbuffer output
- $SIG{'INT'} = 'interrupt'; # catch interrupt
- $proc = $files = $tcp = $udp = 0;
- $progress="/";
- open(P, "$LSOF -HPF0 -r $RPT|") || die "can't open pipe to $LSOF\n";
-
- LSOF_LINE:
-
- while (<P>) {
- chop;
- if (/^m/) {
-
- # A marker line signals the end of an lsof repetition.
-
- printf "%s Processes: %5d, Files: %6d, TCP: %6d, UDP: %6d\r",
- $progress, $proc, $files, $tcp, $udp;
- $proc = $files = $tcp = $udp = 0;
- if ($progress eq "/") { $progress = "\\"; } else { $progress = "/"; }
- next LSOF_LINE;
- }
- if (/^p/) {
-
- # Count process.
-
- $proc++;
- next LSOF_LINE;
- }
- if (/^f/) {
-
- # Count files.
-
- $files++;
- @F = split("\0", $_, 999);
- foreach $i (0 .. ($#F - 1)) {
-
- # Search for protocol field.
-
- if ($F[$i] =~ /^P(.*)/) {
-
- # Count instances of TCP and UDP protocols.
-
- if ($1 eq "TCP") { $tcp++; }
- elsif ($1 eq "UDP") { $udp++; }
- next LSOF_LINE;
- }
- }
- }
- }
-